package com.example.export.service.impl;

import com.example.export.pojo.JxdGroupAO;
import com.example.export.service.JxdNewYnService;
import com.example.export.util.ExcelUtils;
import com.example.export.util.ResultAo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class JxdNewYnServiceImpl implements JxdNewYnService {


    private static ByteArrayOutputStream outputStream;

    static {
        try {
            InputStream rawInputStream = ExcelUtils.getTemplateStream("/templates/sspkjbjxdnew.xls");
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = rawInputStream.read(buffer)) > -1) {
                outputStream.write(buffer, 0, len);
            }
            outputStream.flush();
        } catch (Exception e) {
            log.error("[税收票款结报缴销单]缓存excel流异常", e);
        }
    }

    @SneakyThrows
    @Override
    public ResultAo getJxdWorkbookNewYn() {
        HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(outputStream.toByteArray()));

        List<JxdGroupAO> groups = new ArrayList<>();
        JxdGroupAO jxdGroupAO = new JxdGroupAO();
        jxdGroupAO.setSe(BigDecimal.ONE);
        jxdGroupAO.setSl(2);
        groups.add(jxdGroupAO);
        this.setDataTwo(workbook, groups);
        return new ResultAo("", workbook).success();
    }


    private void setDataTwo(HSSFWorkbook workbook, List<JxdGroupAO> groups) {
        int dataRow = 2;
        Sheet sheet = workbook.getSheetAt(0);

        if (groups.size() > 0) {
            sheet.shiftRows(dataRow, sheet.getLastRowNum(), groups.size() - 1, true, false);

            int totalSl = 0;
            BigDecimal totalSe = BigDecimal.ZERO;
            Row targetRow = sheet.getRow(dataRow - 1);

            for (int i = 0; i < groups.size(); i++) {
                Row row = sheet.createRow(dataRow);
                writeRowTwo(row, groups.get(i), targetRow, i + 1);
                ++dataRow;
                totalSl = totalSl + groups.get(i).getSl();
                totalSe = totalSe.add(groups.get(i).getSe());
            }
            // 合计
            writeFooterRowTwo(sheet.createRow(dataRow), totalSl, totalSe, targetRow);
        }
    }

    private void writeFooterRowTwo(Row row, Integer totalSl, BigDecimal totalSe, Row targetRow) {
        Cell cell0 = row.createCell(0);
        Cell cell1 = row.createCell(1);
        Cell cell2 = row.createCell(2);
        Cell cell3 = row.createCell(3);
        Cell cell4 = row.createCell(4);
        Cell cell5 = row.createCell(5);
        Cell cell6 = row.createCell(6);
        Cell cell7 = row.createCell(7);

        cell0.setCellStyle(targetRow.getCell(0).getCellStyle());
        cell1.setCellStyle(targetRow.getCell(1).getCellStyle());
        cell2.setCellStyle(targetRow.getCell(2).getCellStyle());
        cell3.setCellStyle(targetRow.getCell(3).getCellStyle());
        cell4.setCellStyle(targetRow.getCell(4).getCellStyle());
        cell5.setCellStyle(targetRow.getCell(5).getCellStyle());
        cell6.setCellStyle(targetRow.getCell(6).getCellStyle());
        cell7.setCellStyle(targetRow.getCell(7).getCellStyle());

        cell0.setCellValue("合计：");
        cell1.setCellValue("");
        cell2.setCellValue("");
        cell3.setCellValue("");
        cell4.setCellValue("");
        cell5.setCellValue("");
        cell6.setCellValue(totalSl);
        cell7.setCellValue(totalSe.toString());

    }

    private void writeRowTwo(Row row, JxdGroupAO data, Row targetRow, int index) {
        Cell cell0 = row.createCell(0);
        Cell cell1 = row.createCell(1);
        Cell cell2 = row.createCell(2);
        Cell cell3 = row.createCell(3);
        Cell cell4 = row.createCell(4);
        Cell cell5 = row.createCell(5);
        Cell cell6 = row.createCell(6);
        Cell cell7 = row.createCell(7);
        if (targetRow != null) {
            cell0.setCellStyle(targetRow.getCell(0).getCellStyle());
            cell1.setCellStyle(targetRow.getCell(1).getCellStyle());
            cell2.setCellStyle(targetRow.getCell(2).getCellStyle());
            cell3.setCellStyle(targetRow.getCell(3).getCellStyle());
            cell4.setCellStyle(targetRow.getCell(4).getCellStyle());
            cell5.setCellStyle(targetRow.getCell(5).getCellStyle());
            cell6.setCellStyle(targetRow.getCell(6).getCellStyle());
            cell7.setCellStyle(targetRow.getCell(7).getCellStyle());
        }
        cell0.setCellValue(index);
        cell1.setCellValue(data.getPzzgbm());
        cell2.setCellValue(data.getPztt());
        cell3.setCellValue(data.getStart());
        cell4.setCellValue(data.getEnd());
        cell5.setCellValue(data.getStatus());
        cell6.setCellValue(data.getSl());
        cell7.setCellValue(data.getSe().toString());
    }

}
